clear all
set more off
cd "/homes/nber/yasenov"

use "~/matched_20_30.dta"

*************************
*** SAMPLE SELECTION ****
*************************
sum age_A

keep if bpl_A<15000 | bpl_A==90011 | bpl_A==90021 | bpl_A==90022
keep if age_A >= 8 & age_A <= 45  
drop if labforce_B == 1 
drop if empstat_B == 13 
drop if classwkr_B == 29
keep if school_B == 1

sum age_A

************************
*** 1910 BOUNDARIES ****
************************
gen county1920 = (statefip_A*1000) + (county_A/10)
replace county1920=41061 if county1920==41060.5

gen county = (statefip_B*1000) + (county_B/10)
replace county=41061 if county==41060.5

foreach j of varlist county county1920 {

	* Arizona
	replace `j' = 4009 if `j' == 4011

	* Louisiana
	replace `j' = 22097 if `j' == 22039
	replace `j' = 22019 if `j' == 22011 | `j' == 22003 | `j' == 22053

	* New Mexico
	replace `j' = 35017 if `j' == 35023
	replace `j' = 35019 if `j' == 35011
	replace `j' = 35005 if `j' == 35025

	* Nevada
	replace `j' = 32013 if `j' == 32027
	replace `j' = 32009 if `j' == 32021

	* Oklahoma
	replace `j' = 40031 if `j' == 40033

	* Oregon
	replace `j' = 41013 if `j' == 41031 | `j' == 41017

	* Texas
	replace `j' = 48141 if `j' == 48229 | `j' == 48109
	replace `j' = 48137 if `j' == 48385
	replace `j' = 48355 if `j' == 48273 | `j' == 42849
	replace `j' = 48061 if `j' == 48489
	replace `j' = 48427 if `j' == 48407

	* Utah
	replace `j' = 49047 if `j' == 49009
	replace `j' = 49051 if `j' == 49013
}

**************
*** MERGE ****
**************

merge m:1 county using "~/aggregate_nber.dta", keep(1 3) 
tab county if _merge == 1, sort
tab county if _merge == 2, sort
drop _merge

drop sea statefip pop30
compress

replace miles_to_mex = miles_to_mex / 100

rename occ1950_A occ19501930  // this is 1920
merge m:1 occ19501930 using "~/occ_wage.dta", keep(1 3)
tab occ19501930 if _merge == 1, sort
drop _merge
rename wage30 wage30_1920
rename occ19501930 occ1950_A

rename occ1950_B occ19501930   // this is 1930
merge m:1 occ19501930 using "~/occ_wage.dta", keep(1 3)
tab occ19501930 if _merge == 1, sort
drop _merge
rename wage30 wage30_1930
rename occ19501930 occ1950_B  

replace wage30_1920 = . if occ1950_A > 970
replace wage30_1930 = . if occ1950_B > 970

label var mex_share10 "Mex Share 1910"
label var miles_to_mex "Miles to Mex"
*label var miles_to_mex_interact "Miles to Mex IV"
label var rail2 "Railroad"
label var rail_interact "Railroad IV"
label var pctdem "\% Democrats"
label var pctdem_interact "\% Democrats IV"
label var close_to_rr "Close to RR"
label var close_to_rr_interact "Close to RR IV"

	
***************************
*** INDUSTRY GROUPINGS ****
***************************

gen ind1950 = 1 * (ind1950_B >= 105 & ind1950_B <= 126) + ///
	2 * (ind1950_B >= 206 & ind1950_B <= 239) + ///
	3 * (ind1950_B == 246) + ///
	4 * (ind1950_B >= 306 & ind1950_B <= 499) + ///
	5 * (ind1950_B >= 506 & ind1950_B <= 598) + ///
	6 * (ind1950_B >= 606 & ind1950_B <= 699) + ///
	7 * (ind1950_B >= 716 & ind1950_B <= 756) + ///
	8 * (ind1950_B >= 806 & ind1950_B <= 817) + ///
	9 * (ind1950_B >= 826 & ind1950_B <= 849) + ///
	10 * (ind1950_B >= 856 & ind1950_B <= 859) + ///
	11 * (ind1950_B >= 868 & ind1950_B <= 899) + ///
	12 * (ind1950_B >= 906 & ind1950_B <= 946) + ///
	13 * (ind1950_B > 946 | ind1950_B)
	
label define inds_lbl 1 "Agriculture, Forestry, and Fishing" ///
	2 "Mining" ///
	3 "Construction" ///
	4 "Manufacturing" ///
	5 "Transportation, Communication, and Other Utilities" ///
	6 "Wholesale and Retail Trade" ///
	7 "Finance, Insurance, and Real Estate" ///
	8 "Business and Repair Services" ///
	9 "Personal services" ///
	10 "Entertainment and Recreation Services" ///
	11 "Professional and Related Services" ///
	12 "Public Administration" ///
	13 "Other"
label values ind1950 inds_lbl

tab ind1950, m
tab ind1950_B if mi(ind1950) | ind1950 == 0

drop ind1950_B

*****************
*** OUTCOMES ****
***************** 
*gen byte migr_20_30 = statefip_A != statefip_B
gen byte migr2_20_30 = (county_A != county_B) & (statefip_A == statefip_B)
gen byte stayer_20_30 = statefip_A == statefip_B
gen byte stayer2_20_30 = county_A == county_B

*label var migr_20_30 "in-migration from other state"
label var migr2_20_30 "state1920 = state1930 & county1920 != county1930"
label var stayer_20_30 "state1920 = state1930"
label var stayer2_20_30 "county1920 = county1930"

gen dempl_alternative = (classwkr_B >= 11 & classwkr_B <=29) - (classwkr_A >= 11 & classwkr_A <=29)

gen dwage = wage30_1930 - wage30_1920

************************
*** OTHER VARIABLES ****
************************
gen byte married_A = marst_A == 1 | marst_A == 2
gen byte literate_A = lit_A == 4
gen byte black = race_A == 200

compress

*******************
*** DATA CHECK ****
*******************
sum migr* stay* married* literate* black

tab stayer*, m

********************
*** REGRESSIONS ****
********************
global ind_controls age_A married_A black literate_A c.age_A#c.age_A c.age_A#i.black c.age_A#i.married_A i.black#i.married_A c.age_A#i.literate_A i.literate_A#i.black i.literate_A#i.married
global cnty_controls lnpop30 s_young s_agric s_mf s_urban s_illiterate dustbowl extreme_phdi pop_dens
global more_controls $cnty_controls rldf3329 c.rldf3329#c.rldf3329 miles_to_mex c.miles_to_mex#c.miles_to_mex c.miles_to_mex#c.rldf3329 aa relief loan death_rate rldf3933 c.rldf3933#c.rldf3933

global options cluster(county) absorb(statefip_A)

eststo clear
eststo: reghdfe migr2_20_30 mex_share10 $ind_controls $more_controls, $options
qui estadd ysumm
eststo: reghdfe migr2_20_30 rail_interact rail2 $ind_controls $more_controls, $options
qui estadd ysumm
eststo: reghdfe migr2_20_30 close_to_rr_interact close_to_rr $ind_controls $more_controls, $options
qui estadd ysumm
eststo: reghdfe stayer_20_30 mex_share10 $ind_controls $more_controls, $options
qui estadd ysumm
eststo: reghdfe stayer_20_30 rail_interact rail2 $ind_controls $more_controls, $options
qui estadd ysumm
eststo: reghdfe stayer_20_30 close_to_rr_interact close_to_rr $ind_controls $more_controls, $options
qui estadd ysumm

esttab using "~/fals.tex", ///
	keep(mex_share10 rail_interact close_to_rr_interact) ///
    replace se(3) b(3) stats(N ymean, labels("N" "$\bar{Y}$") fmt(0 3)) star(* 0.10 ** 0.05 *** 0.01) nonum nonotes noconstant nogaps label ///
	mtitles("(1)" "(2)" "(3)" "(4)" "(5)" "(6)") /// 
	prehead("{\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}\begin{tabular}{l*{6}{c}}\hline\hline \\ & \multicolumn{3}{c}{\textbf{Panel A: Moved Within State 1920-1930}} & \multicolumn{3}{c}{\textbf{Panel B: Same State 1920-1930}} \\\cmidrule(lr){2-4} \cmidrule(lr){5-7} %") /// ///
	postfoot("")


eststo clear
eststo: reghdfe dempl_alternative mex_share10 $ind_controls $more_controls, $options
qui estadd ysumm
eststo: reghdfe dempl_alternative rail_interact rail2 $ind_controls $more_controls, $options
qui estadd ysumm
eststo: reghdfe dempl_alternative close_to_rr_interact close_to_rr $ind_controls $more_controls, $options
qui estadd ysumm
eststo: reghdfe dwage mex_share10 $ind_controls $more_controls, $options
qui estadd ysumm
eststo: reghdfe dwage rail_interact rail2 $ind_controls $more_controls, $options
qui estadd ysumm
eststo: reghdfe dwage close_to_rr_interact close_to_rr $ind_controls $more_controls, $options
qui estadd ysumm

esttab using "~/fals.tex", ///
	keep(mex_share10 rail_interact close_to_rr_interact) ///
    append se(3) b(3) stats(N ymean, labels("N" "$\bar{Y}$") fmt(0 3)) star(* 0.10 ** 0.05 *** 0.01) nonum nonotes noconstant nogaps label ///
	mtitles("(1)" "(2)" "(3)" "(4)" "(5)" "(6)") /// 
	prehead("\\ & \multicolumn{3}{c}{\textbf{Panel C: $\Delta$ Employment 1920-1930}}  & \multicolumn{3}{c}{\textbf{Panel D: $\Delta$ Wage 1920-1930}} \\\cmidrule(lr){2-4} \cmidrule(lr){5-7} %")	 ///
	postfoot("")

eststo clear	
eststo: reghdfe stayer2_20_30 mex_share10 $ind_controls $more_controls, $options
estadd local bslncntrl "X"
estadd local morecntrls "X" 
qui estadd ysumm
eststo: reghdfe stayer2_20_30 rail_interact rail2 $ind_controls $more_controls, $options
estadd local bslncntrl "X"
estadd local morecntrls "X" 
qui estadd ysumm
eststo: reghdfe stayer2_20_30 close_to_rr_interact close_to_rr $ind_controls $more_controls, $options
estadd local bslncntrl "X"
estadd local morecntrls "X" 
qui estadd ysumm

esttab using "~/fals.tex", ///
	keep(mex_share10 rail_interact close_to_rr_interact) ///
    append se(3) b(3) stats(N ymean, labels("N" "$\bar{Y}$") fmt(0 3)) star(* 0.10 ** 0.05 *** 0.01) nonum nonotes noconstant nogaps label ///
	mtitles("(1)" "(2)" "(3)") /// 
	prehead("\\ & \multicolumn{3}{c}{\textbf{Panel E: Same County 1920-1930}} \\\cmidrule(lr){2-4} %")	 ///
	scalars("bslncntrl Baseline Controls" "morecntrls Full Controls")
